//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <ddk.h>

#include "calibrate.h"

//X = Ax + (By) + C
//Y = (Dx) + Ey + F

CalibrationInfo g_Cali = {336, 257, 10, 10, 3217, 3658, 200, 300};
//CalibrationInfo g_Cali = {607, 327, 10, 10, 3145, 3480, 200, 300};

void GetRawCoordinate(int *px, int *py, int X, int Y)
{

}

void GetDisplayPoint(int x, int y, int *pX, int *pY)
{
    *pX = X1 + (x - x1) * (X2 - X1) / (x2 - x1);
    *pY = Y1 + (y - y1) * (Y2 - Y1) / (y2 - y1);
}

int SetCalibrationMatrix(int n, RefPoint P[])
{
    assert(n >= 3);
#if defined(_DBGCALIBRATE)
    for(int i = 0; i < 3; i++) {
        kprintf("Raw (%d, %d)--->(%d, %d)\n", P[i].x, P[i].y, P[i].X, P[i].Y);
    }
#endif

    //Verify
    int x = P[2].x, y = P[2].y;
    int X, Y;

    X = P[0].X + (x - P[0].x) * (P[1].X - P[0].X) / (P[1].x - P[0].x);
    Y = P[0].Y + (y - P[0].y) * (P[1].Y - P[0].Y) / (P[1].y - P[0].y);
    kprintf("Raw (%d, %d)\t=>=>(%d, %d), V(%d, %d)\n", x, y, X, Y, P[2].X, P[2].Y);

#define BIAS(a, b) ((a > b)?(a - b):(b - a))
    if ( (BIAS(X, P[2].X) > 10) || (BIAS(Y, P[2].Y) > 10) ) {
        return -1;//fail
    }

    x1 = P[0].x;
    y1 = P[0].y;
    X1 = P[0].X;
    Y1 = P[0].Y;

    x2 = P[1].x;
    y2 = P[1].y;
    X2 = P[1].X;
    Y2 = P[1].Y;
    return 0;//success
}
